{{extend "layout.html"}}
{{block css}}
<link href="{{=url_for_static('jquery-file-upload/css/jquery.fileupload.css')}}" rel="stylesheet" />
<link href="{{=url_for_static('loading.css')}}" rel="stylesheet" />
{{end}}

{{block breadnav}}
<li><a href="/">Home</a>
</li>
<li class="active">{{=breadnav}}</li>
{{end}} {{block content}}
{{import urllib}}
<input type="hidden" id="hid-bug-id" value="{{=bug_id}}" />
<div class="row">
    <div class="col-lg-12">
        <form role="form" class="form-horizontal">
            <div class="form-group">
                <div class="col-lg-offset-2 col-lg-10">
                    <button type="button" class="btn btn-success" id="btn-save" data-toggle="modal" data-target="#saveModal"><i class="fa fa-save fa-fw"></i> 保存</button>
                    {{if bug_id != 0:}}
                    <button type="button" class="btn btn-danger" id="btn-del"><i class="fa fa-trash-o fa-fw"></i> 删除</button>
                    {{pass}}
                    <button class="btn btn-link" id="btn-back" type="button">返回</button>
                </div>
            </div>

            <div class="form-group">
                <label class="col-lg-2 control-label">标题</label>
                <div class="col-lg-6">
                    <input type="text" class="form-control cj-validator" cj-required id="txt-bug-title" cj-name="BUG 标题" value="{{=bug_title}}" autofocus>
                </div>
            </div>

            <div class="form-group">
                <label class="col-lg-2 control-label">所属项目</label>
                <div class="col-lg-2">
                    <select id="sel-project" class="form-control">
                        <option value="{{=p_default.id}}">{{=p_default.name}}</option>
                        {{for p in projects:}}
                        <option value="{{=p.id}}">{{=p.name}}</option>
                        {{pass}}
                    </select>
                </div>

                <label class="col-lg-2 control-label">严重性</label>
                <div class="col-lg-2">
                    <select id="sel-importance" class="form-control">
                        <option value="{{=bi_default.code}}">{{=bi_default.name}}</option>
                        {{for bi in bug_importances:}}
                        <option value="{{=bi.code}}">{{=bi.name}}</option>
                        {{pass}}
                    </select>
                </div>
            </div>

            <div class="form-group">
                <label class="col-lg-2 control-label">分派给</label>
                <div class="col-lg-2">
                    <select id="sel-assign-to" class="form-control">
                        <option value="{{=assign_default.id}}">{{=assign_default.real_name}}</option>
                        {{for assign_user in bug_assign_to:}}
                        <option value="{{=assign_user.id}}">{{=assign_user.real_name}}</option>
                        {{pass}}
                    </select>
                </div>

                <label class="col-lg-2 control-label">状态</label>
                <div class="col-lg-2">
                    <select id="sel-state" class="form-control">
                        <option value="{{=bs_default.code}}">{{=bs_default.name}}</option>
                        {{for bs in bug_states:}}
                        <option value="{{=bs.code}}">{{=bs.name}}</option>
                        {{pass}}
                    </select>
                </div>
            </div>

            <div class="form-group">
                <label class="col-lg-2 control-label">BUG 描述</label>
                <div class="col-lg-6">
                    <textarea class="form-control cj-validator" id="txt-bug-desc" rows="6" cj-name="BUG 描述" cj-required>{{=bug_desc}}</textarea>
                </div>
            </div>

            {{if bug_id != 0:}} {{for c in comments:}}
            <div class="form-group">
                <label class="col-lg-2 control-label">BUG 备注
                    <br>{{=c.create_user.name}}
                    <br>{{=c.create_time.strftime("%Y-%m-%d %H:%M:%S")}}
                </label>
                <div class="col-lg-6">
                    <textarea class="form-control" rows="4" readonly>{{=c.comment}}</textarea>
                </div>
            </div>
            {{pass}}
            <div class="form-group">
                <label class="col-lg-2 control-label">BUG 备注</label>
                <div class="col-lg-6">
                    <textarea class="form-control" id="txt-bug-comment" rows="6"></textarea>
                </div>
            </div>
            {{pass}}

            <div class="form-group">
                <div class="col-lg-offset-2 col-lg-10">
                    <span class="btn btn-primary fileinput-button">
                        <i class="glyphicon glyphicon-plus"></i>
                        <span>上传 附件</span>
                        <input type="file" name="file-bug-attachment" data-url="/bug/upload-attachment" id="bug-attachment-upload">
                    </span>
                </div>
            </div>

            <div class="form-group">
                <div class="col-lg-offset-2 col-lg-8">
                    <table class="table table-striped" id="tb-attachments">
                        <thead>
                            <th class="hidden"></th>
                            <th>#</th>
                            <th>文件名</th>
                            <th>预览</th>
                            <th>下载</th>
                            <th>删除</th>
                        </thead>
                        <tbody>
                            {{order = 1}} {{for ba in bug_attachments:}}
                            <tr>
                                <td class="hidden">{{=ba.id}}</td>
                                <td>{{=order}}</td>
                                <td>{{=ba.name}}</td>
                                <td>
                                {{
                                  path=urllib.quote(ba.path.encode("utf-8"))
                                }}
                                    <button type="button" data-toggle="modal" data-target="#myModal" class="btn btn-link" onclick="review_attachment('/{{=path}}');"><i class="fa fa-download fa-fw"></i>预览
                                    </button>
                                </td>
                                <td>
                                    <button type="button" class="btn btn-link" onclick="download_attachment({{=ba.id}});"><i class="fa fa-download fa-fw"></i> 下载</button>
                                </td>
                                <td>
                                    <button type="button" class="btn btn-danger btn-xs" onclick="del_attachment(this);"><i class="fa fa-trash-o fa-fw"></i> 删除</button>
                                </td>
                            </tr>
                            {{pass}}
                        </tbody>
                    </table>
                </div>
            </div>

        </form>
    </div>
    <!-- col-lg-12 -->
</div>
<!-- row -->

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">图片预览</h4>
            </div>
            <div class="modal-body">
                <img id="review_image" src="" />
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<hr>{{if bug_id != 0:}}
<h3>BUG 修改履历:</h3>
<div class="row">
    <div class="col-lg-12">
        <table class="table table-striped">
            <thead>
                <th>#</th>
                <th>FROM</th>
                <th>TO</th>
                <th>状态</th>
                <th>严重性</th>
                <th>更新时间</th>
            </thead>
            <tbody>
                {{order = 1}} {{for bh in bug_histories:}}
                <tr>
                    <td>{{=order}}</td>
                    <td>{{=bh.assign_from_user.real_name}}</td>
                    <td>{{=bh.assign_to_user.real_name}}</td>
                    <td>{{=bh.bug_state.name}}</td>
                    <td>{{=bh.bug_importance.name}}</td>
                    <td>{{=bh.update_time.strftime("%Y-%m-%d %H:%M:%S")}}</td>
                </tr>
                {{order += 1}} {{pass}}
            </tbody>
        </table>
    </div>
</div>
{{pass}}

<!-- 保存时禁止其它操作 -->
<div class="modal fade" id="saveModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="spinner">
        <div class="rect1"></div>
        <div class="rect2"></div>
        <div class="rect3"></div>
        <div class="rect4"></div>
        <div class="rect5"></div>
    </div>
    <div class="container">
        <div class="well well-lg">
            <p class="text-center text-success h1">正在保存，请稍候！</p>
        </div>
    </div>
</div>
{{end}}
{{block js}}
<script type="text/javascript" src="{{=url_for_static('jquery-file-upload/vendor/jquery.ui.widget.js')}}"></script>
<script type="text/javascript" src="{{=url_for_static('jquery-file-upload/jquery.iframe-transport.js')}}"></script>
<script type="text/javascript" src="{{=url_for_static('jquery-file-upload/jquery.fileupload.js')}}"></script>

<script src="{{=url_for_static('chujue-js/chujue_validate.js')}}"></script>
<script src="{{=url_for_static('chujue-js/chujue_ajax.js')}}"></script>
<script src="{{=url_for_static('views_bug/edit.js')}}"></script>

<script type="text/javascript">
$(function() {
    $("#btn-save").click(save_bug);
    $("#btn-del").click(del_bug);
    $("#btn-back").click(function() {
        location.href = "/bug/list";
    });

    init_attachment_upload();

    $('#saveModal').modal({
        backdrop: 'static',
        show: false,
    });
});
</script>
{{end}}
